package com.xiannimei.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.xiannimei.BusinessException;
import com.xiannimei.Constants;
import com.xiannimei.web.MemberProfile;

/**
 * AuthUtil 有关用户权限信息的工具类
 * 
 * 2013-7-9 下午3:38:08
 * 
 * @author xinyuan.guo
 * 
 */
public class AuthUtil {

	public static void setUserDetails(HttpSession session, MemberProfile mp) {
		session.setAttribute(Constants.SESSION_USER_CONTEXT, mp);
	}

	public static MemberProfile getUserDetails(HttpSession session) {
		Object mpObject = session.getAttribute(Constants.SESSION_USER_CONTEXT);
		if (mpObject !=null && mpObject instanceof MemberProfile){
			return (MemberProfile) mpObject;
		} else 
			throw new BusinessException(6050103);
	}

	public static String getServerRootWithContextPath(HttpServletRequest request){
		return "http://".concat(request.getServerName()).concat((request.getServerPort() == 80 ? "" : ":".concat(String.valueOf(request.getServerPort())))).concat(request.getContextPath());
	}

}
